<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\vlink;

use builder\helper\Url;
use builder\form\FormTrait;
use builder\form\util\control\Control;

/**
 * 值连接控件
 * @author cleverstone
 * @since ym2.0
 */
class ValueLink extends Control
{
    use FormTrait;

    /**
     * @var array
     */
    private $_options = [
        'control' => 'value_link',
        'sourceControl' => [],
        'watchScope' => [],
        'targetRoute' => '',
        '__targetScope' => '',
    ];

    /**
     * 源控件
     * @param array $control
     * @return $this
     */
    public function sourceControl(array $control)
    {
        $this->_options['sourceControl'] = $control;
        $this->_options['watchScope'] = array_keys($control);

        return $this;
    }

    /**
     * 连接路由
     * @param array|string $route 路由
     * @return $this
     */
    public function targetRoute($route)
    {
        $this->_options['targetRoute'] = Url::to($route, '');
        return $this;
    }

    /**
     * 连接字段
     * @param string $field 字段
     * @param string $groupField 分组
     * @return $this
     */
    public function targetField($field, $groupField = 'default')
    {
        $this->_options['__targetScope'] = $this->generateJavascriptScope($groupField, $field);

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}